﻿using Business.Core.Annotations;
using Common;
using DataModel;
using Newtonsoft.Json;
using ProjectManager.User;
using ProjectManager.User.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace ProjectManagerSys.backoffice.user
{
    /// <summary>
    /// 用户接口
    /// </summary>
    public class UserMerber : Common.BusinessBase
    {
        [Doc("获取用户部门", Group = "用户管理")]
        public virtual async Task<dynamic> GetDepartAndUser()
        {
            try
            {
                using (var con = Data.DB.GetConnection())
                {
                    var departlist = con.BJSYS_DEPARTMENT.Where(c => c.ACTIVE == 1).ToList();
                    var userlist = con.BJSYS_USERINFO.Where(c=>c.ACTIVE ==1).ToList();
                    var obj = new { departlist = departlist , userlist = userlist };
                    return (JsonConvert.SerializeObject(obj));
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }

        [Doc("判断是否登录", Group = "用户管理")]
        public virtual async Task<dynamic> isLogin()
        {
            int flag = 0;
            AuthInfo authinfo = Logins.Authorize();
            if (!string.IsNullOrEmpty(authinfo.userid))
                flag = 1;
            return JsonConvert.SerializeObject(flag);
        }

        [Doc("获取同一部门用户", Group = "用户管理")]
        public virtual async Task<dynamic> GetUserByDepartId()
        {
            try
            {
                using (var con = Data.DB.GetConnection())
                {
                    var userlist = new List<BJSYS_USERINFO>();
                    AuthInfo authinfo = Logins.Authorize();
                    if (authinfo.isadmin == 1)
                    {
                        userlist = con.BJSYS_USERINFO.Where(c => c.ACTIVE == 1).OrderBy(c => c.SORTINDEX).ToList();
                    }
                    else
                    {
                        var arr_depart = authinfo.departid.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                        userlist = con.BJSYS_USERINFO.Where(c => c.ACTIVE == 1 && arr_depart.Contains(c.DEPARTID)).OrderBy(c => c.SORTINDEX).ToList();
                    }
                    
                    return (JsonConvert.SerializeObject(userlist));
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }

        [Doc("获取部门的审批人", Group = "用户管理")]
        public virtual async Task<dynamic> GetDepartApproval()
        {
            try
            {
                using (var con = Data.DB.GetConnection())
                {
                    AuthInfo authinfo = Logins.Authorize();
                    var userlist = con.BJSYS_USERINFO.Where(c => c.ACTIVE == 1 && c.DEPARTID==authinfo.departid && c.CODE==1).ToList();
                    var userids = new string[] { };
                    var realnames = new string[] { } ;
                    if (userlist.Count > 0)
                    {
                        userids = userlist.Select(c => c.ID).ToArray();
                        realnames = userlist.Select(c => c.REALNAME).ToArray();
                    }
                    var obj = new { userids = userids , realnames = realnames };
                    return (JsonConvert.SerializeObject(obj));
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
